<?php

namespace App\Http\Controllers\Admin;

use App\Models\Agent;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;

class AgentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $keyword=$request->input('keyword','');
        $query=Agent::select();
        if($keyword!=''){
            $query=$query->where('name','like',"%$keyword%");
        }
        $agents=$query->orderBy('created_at','desc')->paginate();
        $agents->appends(['keyword'=>$keyword]);
        $agents->keyword=$keyword;
        return view('Admin.Agent.index',compact('agents'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('Admin.Agent.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $this->validate($request,[
            'name'=>'required'
        ],[
            'name.required'=>'请填写机构名称'
        ]);
        $data=['name'=>$request->name,'contact_man'=>$request->contact_man,'phone'=>$request->phone];
        if($request->hasFile('logo') && $request->file('logo')->isValid()){
            $data['logo']=$request->file('logo')->store('images/agent','ys');
        }
        if(Agent::create($data)){
            die("<script>parent.location.href='".route('agent.index')."'</script>");
//            return redirect()->route('agent.index')->with('success','添加成功');
        }else{
            return redirect()->back()->withInput()->withErrors('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $agent=Agent::findOrFail($id);
        return view('Admin.Agent.edit',compact('agent'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $this->validate($request,[
            'name'=>'required',
        ],[
            'name.required'=>'请填写机构名称'
        ]);
        $agent=Agent::findOrFail($id);
        $agent->name=trim($request->name);
        $agent->contact_man=trim($request->contact_man);
        $agent->phone=trim($request->phone);
        if($request->hasFile('logo') && $request->file('logo')->isValid()){
            $agent->logo=$request->file('logo')->store('images/agent','ys');
        }
        if($agent->save()){
            die("<script>parent.location.href='".route('agent.index')."'</script>");
        }else{
            return redirect()->back()->withInput()->withErrors('添加失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $agent=Agent::findOrFail($id);
        $product=Product::where(['basic_info->agent_id'=>$id])->first();
        if($product!=null){
            return response()->json(['status'=>201,'msg'=>'该机构下有关联的产品,不能直接删除']);
        }
        if($agent->delete()){
            return response()->json(['status'=>200,'msg'=>'删除成功']);
        }else{
            return response()->json(['status'=>202,'msg'=>'删除失败']);
        }
    }

















}
